package com.aimei520.utils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.aimei520.po.admin.Url;
import com.aimei520.po.meun.Menu;
/**
 * 菜单树生成类
 * Title: MenuTree.java
 * Description:  
 * Copyright: Copyright (c) 2018
 * Company: www.storyboy.xyz 
 * @author JiaXun  
 * @date 2018年9月1日  
 * @version 1.0.0
 */
public class MenuTree {
	
	private static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
	private List<Menu> menuCommon = new ArrayList<Menu>();
	private List<Menu> accesssCommon = new ArrayList<Menu>();
	private List<Url> urlCommon = new ArrayList<Url>();
	private List<Menu> result = new ArrayList<Menu>();
	private List<Object> list = new ArrayList<Object>();
 

	private int num;
	public List<Object> menuList(List<Menu> menu){    
		this.menuCommon = menu;
		for (Menu x : menu) {   
			Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
			if(x.getPid()==0){
				mapArr.put("id", x.getId());
				mapArr.put("title", x.getTitle());
				mapArr.put("pid", x.getPid());	
				mapArr.put("icon", x.getIcon());	
				mapArr.put("url", x.getUrl());	
				mapArr.put("child", menuChild(x.getId()));	
				list.add(mapArr);
			}
		}	
		return list;
	}
	
	
	private List<?> menuChild(int id){
		List<Object> lists = new ArrayList<Object>();
		for(Menu a:menuCommon){
			Map<String,Object> childArray = new LinkedHashMap<String, Object>();
			if(a.getPid() == id){
				childArray.put("id", a.getId());
				childArray.put("title", a.getTitle());
				childArray.put("pid", a.getPid());
				childArray.put("icon", a.getIcon());
				childArray.put("url", a.getUrl());	
				childArray.put("child", menuChild(a.getId()));	
				lists.add(childArray);
			}
		}
		return lists;
	}
	
	/**
	 * 生成菜单树
	 * @param menu
	 * @return
	 */
	public List<Menu> menuList2(List<Menu> menu){ 
		menuCommon.addAll(menu);
		for (Menu x : menu) {   
			num += 1;
			if(x.getPid()==0){
				Menu menu_item = new Menu(x.getId(), x.getTitle(), x.getUrl(), x.getIcon(), x.getPid(), x.getVisible());
				result.add(menu_item);
				menu_item.setChildMenu(menuChild2(menu_item));
			}
			
		}	
		return result;
	}
	
	
	private List<Menu> menuChild2(Menu menu){
		List<Menu> lists = new ArrayList<Menu>();
		List<Menu> remove_menu = new ArrayList<Menu>();
		for(Menu a:menuCommon){
			num += 1;
			if(a.getPid() == menu.getId()){
				Menu menu_item = new Menu(a.getId(), a.getTitle(), a.getUrl(), a.getIcon(), a.getPid(), a.getVisible());
				lists.add(menu_item);
				remove_menu.add(menu_item);
				menu.setChildMenu(menuChild2(menu_item));
			}
		}
		menuCommon.removeAll(remove_menu);
		return lists;
	}
	
	
	/**
	 * 生成权限树
	 * @param menu
	 * @return
	 */
	public List<Menu> urlList(List<Url> url,List<Menu> accessUrls){ 
		accesssCommon = accessUrls;
		urlCommon.addAll(url);
		for (Url x : url) {   
			num += 1;
			if(x.getPid()==0){
				Menu menu_item = new Menu(x.getId(), x.getTitle(), x.getUrl(), x.getIcon(), x.getPid(), x.getVisible());
				for (Menu menu : accesssCommon) {
					if(x.getId() == menu.getId()) {
						menu_item.setPossess(true);
					}
				}
				result.add(menu_item);
				menu_item.setChildMenu(urlChild(menu_item));
			}
			
		}	
		return result;
	}
	
	
	private List<Menu> urlChild(Menu menu){
		List<Menu> lists = new ArrayList<Menu>();
		List<Url> remove_menu = new ArrayList<Url>();
		for(Url a:urlCommon){
			num += 1;
			if(a.getPid() == menu.getId()){
				
				Menu menu_item = new Menu(a.getId(), a.getTitle(), a.getUrl(), a.getIcon(), a.getPid(), a.getVisible());
				for (Menu item : accesssCommon) {
					if(a.getId() == item.getId()) {
						menu_item.setPossess(true);
					}
				}
				lists.add(menu_item);
				Url b = new Url(a.getId(), a.getTitle(), a.getUrl(), a.getIcon(), a.getPid(), a.getVisible(), a.getStatus());
				remove_menu.add(b);
				menu.setChildMenu(urlChild(menu_item));
			}
		}
		urlCommon.removeAll(remove_menu);
		return lists;
	}
}
